/*
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-08 23:07:56
 * @LastEditTime: 2022-05-17 00:37:45
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
 */
import React, { useState } from 'react'
import { Form, Input, Button, Upload, message } from 'antd';
import { getUserSys, updateUserSys } from '@/api/sys'
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import { getItem, setItem } from '@/utils/storage'
import './index.less'

function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

function beforeUpload(file) {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('You can only upload JPG/PNG file!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('Image must smaller than 2MB!');
  }
  return isJpgOrPng && isLt2M;
}


export default function Means() {

  const [form] = Form.useForm();
  getUserSys().then(res => {
    const { username, password, avatar } = res.data
    form.setFieldsValue({ username, password }) //对象形式(user:{data})
    setImgUrl('http://47.93.114.103:6688/' + avatar)
  })

  const onFinish = async (values) => {
    await updateUserSys(values)
    console.log('Success:', values);
  };

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };
  const [loading, setLoading] = useState()
  const [imgUrl, setImgUrl] = useState()
  const handleChange = info => {
    if (info.file.status === 'uploading') {
      setLoading(true)
      return;
    }
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      getBase64(info.file.originFileObj, imageUrl => {
        setImgUrl(imageUrl)
        setItem('info', { ...getItem('info'), avatar: info.file.response.data.filePath })
      }

      );
    }
  };
  const uploadButton = (
    <div>
      {loading ? <LoadingOutlined /> : <PlusOutlined />}
      <div style={{ marginTop: 8 }}>上传</div>
    </div>
  )
  return (

    <div div className='container-box' >
      <Form
        name="basic"
        form={form}
        wrapperCol={{ span: 6 }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
      >
        <Form.Item
          label="修改用户名"
          name="username"
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="修 改 密 码"
          name="password"
        >
          <Input.Password />
        </Form.Item>
        <Form.Item wrapperCol={{ offset: 6 }}>
          <Button type="primary" htmlType="submit">
            确 定
          </Button>
        </Form.Item>
      </Form>

      <Upload
        name="avatar"
        listType="picture-card"
        className="avatar-uploader"
        showUploadList={false}
        headers={{ 'cms-token': getItem('info')['cms-token'] }}
        action="api/upload"
        beforeUpload={beforeUpload}
        onChange={handleChange}
      >
        {imgUrl ? <img src={imgUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
      </Upload>
    </div>
  )
}
